using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace NUnitHelper
{
    public partial class NUnitHelper : Form
    {
        public NUnitHelper()
        {
            InitializeComponent();
        }

        private void btnCompile_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            try
            {
                string filePath = browseWebsite.Folder;
                if (ValidWebsite(filePath))
                {
                    textBox1.Text = "Compiling...";
                    textBox1.Text = Managers.CompilerManager.ReCompile(filePath);
                }
                else
                {
                    MessageBox.Show(String.Format("'{0}' is not an ASP.Net website.", filePath), "ZOMG!");
                }
            }
            catch (Exception ex)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("An error occurred:");
                sb.AppendLine();
                sb.AppendLine("Message:");
                sb.AppendLine(ex.Message);
                sb.AppendLine();
                sb.AppendLine("Stack trace:");
                sb.AppendLine(ex.StackTrace);

                if (ex.Message.Contains("another process"))
                {
                    sb.AppendLine();
                    sb.AppendLine();

                    sb.Append("Yahweh guesses that some resource hasn't been released. ");
                    sb.Append("If the resource is a database, make sure you explicitly close your ");
                    sb.Append("connection in the Visual Studio IDE's \"Database Explorer\". Also ");
                    sb.Append("try closing and reopening NUnit to free whatever resources it's using ");
                    sb.Append("as well.");

                    sb.AppendLine();
                    sb.AppendLine();

                    sb.Append("If you get this error repeatedly, then your code probably ");
                    sb.Append("opens a connection to a database and throws an exception before the ");
                    sb.Append("connection closes. Always make sure you close your exception when ");
                    sb.Append("you're finished with it, and always use try-finally blocks around ");
                    sb.Append("any database transactions.");

                }
                textBox1.Text = sb.ToString();
            }
        }

        private bool ValidWebsite(string filePath)
        {
            if (filePath.Length > 0)
            {
                // this function checks for the existence of an App_Code directory.
                // If there is one, then the folder is definitely a website, so it can
                // be compiled.
                return Directory.Exists(filePath + @"\App_Code");
            }
            else
            {
                return false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataManagerHelper data = new DataManagerHelper();
            data.Show();
        }

        private void NUnitHelper_Load(object sender, EventArgs e)
        {
            txtTempWebsite.Text = Managers.CompilerManager.GetTempLocation(@"\bin\App_Code.dll");
        }
    }
}